define(window.backboneCommon.getViewDefine("agent.index"), function ($,require,Collection,tpl,ViewDetail) {
    var backboneView= Backbone.View.extend({
        events: {
            "click #btn_add": "addAgent",
            "click .edit-agent": "editAgent"
        },
        initialize: function () {
            this.listenTo(this.model, 'bind:PageList', this.bindList);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(){
            this.elementModel={};
            this.extObject={};
            this.pageData = {};
            this.elementModel.table=$("#tb_list");
            $.ExtAssist.navBread.nav([{name:"代理商管理"},{name:"代理商列表"}]);
            this.initViewAttach();
            this._initPagePower();
        },
        _initPagePower:function(){
            var self=this;
            $.ExtAssist.rolePower.operatePage("list",[],function(json){
                self.pageData.power={
                };
                self._initBodyMain();
            });
        },
        _initBodyMain:function(){
            this._initInputSearch();
            this._initPageList();
        },
        initViewAttach:function () {
            var self=this;
            self.viewAttach={};
            self.viewAttach.detail=new ViewDetail({pElement:"#container_Detail",
                submitCallback:function (isAdd,info) {
                    if(isAdd){
                        self.refreshList();
                    }else{
                        var tr=$("#userid_"+info.id).parent();
                        var row = $("#tb_listUser").ExtDataTable().getRowInfo(tr);
                        row.data()["status"]=info.status;
                        self._bindTableStatus(tr,info.status)
                    }
                }
            });
        },
        _initInputSearch:function () {
            var self=this;
            self.extObject.inputSearch=$("#panel_inputSearch").ExtInputSearch({
                placeholder:"代理商ID,代理商名称",
                searchCallback:function () {
                    self.extObject.table.refresh()
                }
            })
        },
        _getSearchParam:function(){
            var param={};
            var keyword = this.extObject.inputSearch.getValue();
            if (!$.ExtCommon.isCheckEmpty(keyword)) {param.search = keyword;}
            return param;
        },
        _initPageList: function () {
            var self=this;
            var options={
                columns: [
                    {data: "id",class: "w-60"},
                    {data: "name",class: "w-min-100"},
                    {data: "status",class: "w-150"},
                    {data: "commission",class: "w-150 num-int"},
                    {data: "customerNum",class: "w-150 num-int"},
                    {data: "createTime",class: "w-120"}
                ],
                infoExt:{
                    ajaxDataPara:function() {
                        var param = {};
                        param = $.ExtCommon.initMergeJson(param, self._getSearchParam());
                        self.pageData.listParam = $.ExtCommon.getJsonClone(param);
                        return param;
                    },
                    ajaxWholeCallback:function(param){
                        self.collection.getPageList(param)
                    }
                },
                createdRow: function (row, data) {
                    var elementTd = $('td', row);
                    elementTd.eq(1).attr("id", "agentid_" + data.id).html('<a class="btn-operate longterm edit-agent" href="javascript:">' + data.name + '</a>');
                    self._bindTableStatus(row,data.status);
                },
                drawCallback: function () {
                    $.ExtNumeric.initAll();
                    $.ExtAssist.urlParam.setUrl();
                }
            };
            self.extObject.table=self.elementModel.table.ExtDataTable(options);
        },
        _bindTableStatus:function(tr,status){
            var isStart=status.toString() === "1";
            $('td', tr).eq(2).html($.ExtCommon.getStringConnect('<div class="{0}" >{1}</div>', [isStart? "text-green" : "text-red", isStart ? "有效" : "暂停中"]));
        },
        bindList:function(){
            var data=this.model.get("dataPageList");
            this.extObject.table.ajaxWholeRefreshData(data);
        },
        refreshList: function () {this.extObject.table.refresh()},
        addAgent:function () {
            this.viewAttach.detail.init({isAdd:true});
        },
        editAgent:function (e) {
            var tr = $(e.currentTarget).parents("tr:first");
            var rowData = this.extObject.table.getRowInfo(tr).data();
            this.viewAttach.detail.init({isAdd:false, id:rowData.id})
        }
    });
    var viewModel=function(){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({el: "#container_bodyMain",collection:this.collection,model:this.model});
    };
    viewModel.prototype={
        initView:function () {
            this.view.initMain();
        }
    };
    return viewModel;
});